/*
 * Created by JFormDesigner on Tue Jul 07 17:59:39 CST 2020
 */
package entity.JFrame;

import entity.Model.User;
import utils.JDBCUtil;

import java.awt.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.*;

/**
 * @author Brainrain
 */
public class UserLoginJFrame extends JFrame {
    private static JLabel label2;
    private static JLabel label3;
    private static JTextField textField1;
    private static JButton button1;
    private static JButton button2;
    private static JPasswordField passwordField1;
    private static Connection con;
    private static PreparedStatement preparedStatement;
    private ResultSet re;

    public UserLoginJFrame() {
        setLayout(null);
        setSize(500, 450);
        setTitle("图书管理系统");
        initComponents();
        setLocationRelativeTo(null);
    }

    private void initComponents() {
        try {
            con = JDBCUtil.getConnect();
        } catch (Exception e) {
        }
        label2 = new JLabel();
        label3 = new JLabel();
        textField1 = new JTextField();
        button1 = new JButton();
        button2 = new JButton();
        passwordField1 = new JPasswordField();
        //======== this ========
        setBackground(new Color(102, 153, 255));
        setForeground(SystemColor.textHighlight);
        setIconImage(new ImageIcon("C:\\Users\\429762125\\Desktop\\\u5c0f\u7a0b\u5e8f\u56fe\u7247\\\u5496\u5561.jpg").getImage());
        setFont(new Font("Dialog", Font.ITALIC, 12));
        Container contentPane = getContentPane();
        contentPane.setLayout(null);
        //---- label2 ----
        label2.setText("\u8d26\u53f7\uff1a");
        label2.setForeground(Color.blue);
        label2.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 20));
        contentPane.add(label2);
        label2.setBounds(135, 65, 75, 55);
        //--------label3 -----------
        label3.setText("\u5bc6\u7801\uff1a");
        label3.setForeground(Color.blue);
        label3.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 20));
        contentPane.add(label3);
        label3.setBounds(130, 130, 75, 50);
        //--------textField1---------
        textField1.setBounds(195, 75, 155, 35);
        textField1.setFont(new Font("黑体", Font.BOLD, 20));
        contentPane.add(textField1);
        //---- button1 ----
        button1.setText("\u767b\u5f55");
        button1.setBackground(new Color(153, 255, 0));
        button1.setEnabled(true);
        button1.setFont(new Font("\u6977\u4f53", Font.BOLD, 15));
        button1.setBorder(null);
        contentPane.add(button1);
        //按钮失去焦点
        button1.setFocusPainted(false);
        add(button1);
        //登录按钮事件监听
        button1.addActionListener((e) -> {
            String userinputid = textField1.getText();
            String userinputpassword = String.valueOf(passwordField1.getPassword());
            if (userinputid.equals("") || userinputpassword.equals("")) {
                JOptionPane.showMessageDialog(this, "不能输入空值", "nlll", JOptionPane.WARNING_MESSAGE);
            } else {
                String sql = "select * from users_table where name=?";
                try {
                    preparedStatement = con.prepareStatement(sql);
                    preparedStatement.setString(1, userinputid);
                    re = preparedStatement.executeQuery();
                    String pass = "";
                    while (re.next()) {
                        pass = re.getString("password");
                        System.out.println("密码：" + re.getString("password"));
                    }
                    System.out.println("pass:" + pass);
                    if (re != null) {
                        if (pass.equals(userinputpassword)) {
                            JOptionPane.showMessageDialog(this, "登录成功", "success", JOptionPane.WARNING_MESSAGE);
                            User user = new User(userinputid, pass);
                            user.setName(userinputid);
                            System.out.println("userinputid+" + userinputid);
                            System.out.println("pass+" + pass);
                            user.setPassword(pass);
                            //new DetailJFrame();
                            new Detail();
                            this.dispose();
                            JDBCUtil.close(preparedStatement, re, con);
                        } else {
                            JOptionPane.showMessageDialog(this, "密码错误", "fail", JOptionPane.WARNING_MESSAGE);
                        }
                    } else {
                        JOptionPane.showMessageDialog(this, "不存在该账号", "fail", JOptionPane.WARNING_MESSAGE);
                    }
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        });
        button1.setBounds(165, 195, 85, 50);
        //---- button2 ----
        button2.setText("\u6ce8\u518c");
        button2.setBackground(new Color(153, 255, 0));
        button2.setEnabled(true);
        button2.setFont(new Font("\u6977\u4f53", Font.BOLD, 15));
        button2.setBorderPainted(false);
        contentPane.add(button2);
        //注册按钮事件监听
        button2.addActionListener((e) -> {
            System.out.println("button2");
            this.dispose();
            NewUserJFrame f = new NewUserJFrame();
        });
        button2.setBounds(295, 195, 80, 50);
        contentPane.add(passwordField1);
        passwordField1.setBounds(195, 140, 155, 35);
        contentPane.setPreferredSize(new Dimension(510, 355));
        setSize(510, 355);
        setLocationRelativeTo(getOwner());
    }
}

